package com.jeequan.jeepay.service.impl;

//package .service.impl;
import cn.hutool.core.util.StrUtil;
import com.jeequan.jeepay.core.entity.PayRedoOrder;
import com.jeequan.jeepay.service.PayRedoOrderService;
import com.jeequan.jeepay.service.mapper.PayRedoOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
//import .entity.PayRedoOrder;
//import .mapper.PayRedoOrderMapper;
//import .service.PayRedoOrderService;
 /**
 * ;(pay_redo_order)表服务实现类
 * @author : wondream322@gmail.com
 * @date : 2023-8-19
 */
@Service
public class PayRedoOrderServiceImpl implements PayRedoOrderService {
    @Autowired
    private PayRedoOrderMapper payRedoOrderMapper;
    
    /** 
     * 通过ID查询单条数据 
     *
     * @param id 主键
     * @return 实例对象
     */
    public PayRedoOrder queryById(Integer id){
        return payRedoOrderMapper.selectById(id);
    }
    
    /**
     * 分页查询
     *
     * @param payRedoOrder 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    public Page<PayRedoOrder> paginQuery(PayRedoOrder payRedoOrder, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<PayRedoOrder> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(payRedoOrder.getRemark())){
            queryWrapper.eq(PayRedoOrder::getRemark, payRedoOrder.getRemark());
        }
        //2. 执行分页查询
        Page<PayRedoOrder> pagin = new Page<>(current , size , true);
        IPage<PayRedoOrder> selectResult = payRedoOrderMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }
    
    /** 
     * 新增数据
     *
     * @param payRedoOrder 实例对象
     * @return 实例对象
     */
    public PayRedoOrder insert(PayRedoOrder payRedoOrder){
        payRedoOrderMapper.insert(payRedoOrder);
        return payRedoOrder;
    }
    
    /** 
     * 更新数据
     *
     * @param payRedoOrder 实例对象
     * @return 实例对象
     */
    public PayRedoOrder update(PayRedoOrder payRedoOrder){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<PayRedoOrder> chainWrapper = new LambdaUpdateChainWrapper<PayRedoOrder>(payRedoOrderMapper);
        if(StrUtil.isNotBlank(payRedoOrder.getRemark())){
            chainWrapper.set(PayRedoOrder::getRemark, payRedoOrder.getRemark());
        }
        //2. 设置主键，并更新
        chainWrapper.eq(PayRedoOrder::getId, payRedoOrder.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(payRedoOrder.getId());
        }else{
            return payRedoOrder;
        }
    }
    
    /** 
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Integer id){
        int total = payRedoOrderMapper.deleteById(id);
        return total > 0;
    }
}